package cn.com.weather.nf.repository.impl;

import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import cn.com.weather.nf.proj.GlobalConfig;
import cn.com.weather.nf.repository.JdbcDao;

@Repository
public class JdbcDaoImpl implements JdbcDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(JdbcDaoImpl.class );
    
    private JdbcTemplate jdbcTemplate;
    private NamedParameterJdbcTemplate mamedParameterJdbcTemplate;
    
    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
        this.mamedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
    }

    @Override
    public String getUserUid(Long userId) {
        
        String sql = "select uid from wi_user_info a where a.id = ? ";
        LOGGER.debug("sql: {}", sql);
        LOGGER.debug("param: {}", userId);
        
        String result = null;
        try {
            result = this.jdbcTemplate.queryForObject(sql, new Object[] {userId}, String.class);
            LOGGER.debug("result: {}", result);
        } catch (Exception e) {
            LOGGER.debug(GlobalConfig.LOG_DEBUG_PREFIX_1, e.getMessage());
        }
        return result;
    }

    @Override
    public Long getUserScoreId(String userUid, String recordTable, Long recordId) {
        String sql = "select a.user_uid from wi_user_score a " +
                "where a.user_uid = :userUid " +
                "and a.record_table = :recordTable " + 
                "and a.record_id = :recordId ";
       
        MapSqlParameterSource sqlParam = new MapSqlParameterSource("userUid", userUid)
                .addValue("recordTable", recordTable)
                .addValue("recordId", recordId);
        LOGGER.debug("sql: {}", sql);
        LOGGER.debug("param: {}", sqlParam);
        
        Long result = null;
        try {
            List<Long> resultList = this.mamedParameterJdbcTemplate.queryForList(sql, sqlParam, Long.class);
            LOGGER.debug("result: {}", result);
            if (resultList != null && !resultList.isEmpty()) {
                result = resultList.get(0);
            }
        } catch (Exception e) {
            LOGGER.debug(GlobalConfig.LOG_DEBUG_PREFIX_1, e.getMessage());
        }
        return result;
    }

    /**
     * 
     */
    @Override
    public String getRecordFormType(Long recordId) {
        String sql = "select b.`type` from sun_record_form a, sun_template_instances b "+ 
                "where a.post_instance = b.id " +
                "and a.id = ? " ;
        
        LOGGER.debug("sql: {}", sql);
        LOGGER.debug("param: {}", recordId);
        
        String result = null;
        try {
            result = this.jdbcTemplate.queryForObject(sql, new Object[] {recordId}, String.class);
            LOGGER.debug("result: {}", result);
        } catch (Exception e) {
            LOGGER.debug(GlobalConfig.LOG_DEBUG_PREFIX_1, e.getMessage());
        }
        return result;
    }

    @Override
    public void updateRecordScore(Long scoreId, Map<String, Object> param) {
       
        String sql = "UPDATE wi_user_score SET " +
                "create_time = ?, " +
                "score_val = ? " +
                "where id = ? ";

        LOGGER.debug("sql: {}", sql);
        LOGGER.debug("param: {}", param);
        int result = 0;
        try {
            result = this.jdbcTemplate.update(sql, 
                    param.get("create_time"), param.get("score_val"), scoreId
            );
            LOGGER.debug("result: {}", result);
        } catch (Exception e) {
            LOGGER.debug(GlobalConfig.LOG_DEBUG_PREFIX_1, e.getMessage());
        }
    }

    @Override
    public void insertRecordScore(Map<String, Object> values) {

        String sql = "insert into wi_user_score (`user_uid`, `record_table`, "+ 
                " `record_id`, `create_time`, `score_type`, "+ 
                " `record_type_name`, `score_val`, `score_count`, `score_info` "+
                " ) values (:user_uid, :record_table, "+
                " :record_id, :create_time, :score_type, "+ 
                " :record_type_name, :score_val, :score_count, :score_info "+ 
                " ) ";

        LOGGER.debug("sql: {}", sql);
        LOGGER.debug("param: {}", values);
        int result = 0;
        try {
            result = this.mamedParameterJdbcTemplate.update(sql, new MapSqlParameterSource(values));
            LOGGER.debug("result: {}", result);
        } catch (Exception e) {
            LOGGER.debug(GlobalConfig.LOG_DEBUG_PREFIX_1, e.getMessage());
        }
        
    }
    
    
    
    
    
    
}
